﻿<%@ WebHandler Language="C#" Class="list" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using System.Web.Services;
using System.IO;
using System.Drawing;


public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    private StringBuilder sb_1 = null;
    private StringBuilder sb_2 = null;
    private StringBuilder sb_3 = null;
    private StringBuilder sb_4 = null;
    private StringBuilder sb_5 = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest (HttpContext context) {
        string type = PageBase.ext_string(context.Request.Params["type"]);
        switch (type)
        {
 
            //查询类型下拉选项
            case "getselect":
                getselect(context);
                break;
            //查询角色信息
            case "getlist":
                getinfo_list(context);
                break;
            //查询功能信息
            case "getlist_function":
                getlist_function(context);
                break;
            //新增
            case "add":
                add(context);
                break;
            //修改
            case "edit":
                edit(context);
                break;
            //删除
            case "delete":
                delete(context);
                break;
            //启用
            case "start":
                start(context);
                break;
            //根据id获取信息
            case "getlist_byid":
                getlist_byid(context);
                break;
            default:
                //getinfo(context);
                break;
        }
    }

    //查询功能信息
    public void getlist_function(HttpContext context)
    {
        string functionids = PageBase.ext_string(context.Request.Params["functionids"]);
        string sql = "";
        if (functionids != "")
        {
            sql = @"select id,name from sys_function where id in({0}) ltrim(rtrim(isactive))='Y'";
            sql = string.Format(sql,functionids);
        }
        else
        {
            sql = @"select id,name from sys_function where  ltrim(rtrim(isactive))='Y'";
        }
      
        DataTable dt = DBHelper.ExecuteDataTable(sql);

        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    /// <summary>
    /// for循环
    /// </summary>
    /// <param name="yourValue"></param>
    /// <returns></returns>
    public static bool IsRepeat(int[] array)
    {
        for (int i = 0; i < array.Length; i++)
        {
            for (int j = 0; j < array.Length; j++)
            {
                if (j <= i)
                {
                    continue;
                }
                if (array[i] == array[j])
                {
                    return true;
                }
            }
        }
        return false;
    }
   
    //查询图标根据id
    public void getlist_byid(HttpContext context)
    {
        string editid = PageBase.ext_string(context.Request.Params["editid"]);
        string sql = @"select id, rolesname, rolesremark, functionids, functionnames, ltrim(rtrim(isactive)) isactive, 
      createtime, updatetime from [sys_roles] where id={0} ";
        sql = string.Format(sql,editid);
        DataTable dt = DBHelper.ExecuteDataTable(sql);


        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"rolesname\":\"" + dt.Rows[i]["rolesname"] + "\"");
            sb.Append(",\"rolesremark\":\"" + dt.Rows[i]["rolesremark"] + "\"");
            sb.Append(",\"functionids\":\"" + dt.Rows[i]["functionids"] + "\"");
            sb.Append(",\"functionnames\":\"" + dt.Rows[i]["functionnames"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"updatetime\":\"" + dt.Rows[i]["updatetime"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //启用
    public void start(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "update  sys_roles set isactive='Y' where id in ({0})";
        sqldelete = string.Format(sqldelete, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "启用成功";
        }
        else
        {
            flag = false;
            msg = "启用失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //删除
    public void delete(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "update  sys_roles set isactive='N' where id in ({0})";
        sqldelete = string.Format(sqldelete, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "禁用成功";
        }
        else
        {
            flag = false;
            msg = "禁用失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //查询
    public void getinfo_list(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);

        string orderby = "createtime";
        string where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,
          id, rolesname, rolesremark, functionids, functionnames, ltrim(rtrim(isactive)) isactive, 
          createtime, updatetime  from [sys_roles]  where 1=1 ";
        string rolesname =PageBase.ext_string(context.Request.Params["rolesnames"]);
        string sel_isactive =PageBase.ext_string(context.Request.Params["sel_isactive"]);
        //功能名称
        if (!string.IsNullOrEmpty(rolesname))
        {
            where += " and rolesname like N'%" + rolesname + "%'";
        }
        //是否可用
        if (!string.IsNullOrEmpty(sel_isactive))
        {
            where += " and isactive ='" + sel_isactive + "'";
        }
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"rolesname\":\"" + dt.Rows[i]["rolesname"] + "\"");
            sb.Append(",\"rolesremark\":\"" + dt.Rows[i]["rolesremark"] + "\"");
            sb.Append(",\"functionids\":\"" + dt.Rows[i]["functionids"] + "\"");
            sb.Append(",\"functionnames\":\"" + dt.Rows[i]["functionnames"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"updatetime\":\"" + dt.Rows[i]["updatetime"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询类型下拉选项
    public void getselect(HttpContext context)
    {
        string ysdid = PageBase.ext_string(context.Request.Params["ysdid"]);
        string sql = @"select * from [t_type] where ltrim(rtrim(isactive))='Y'";
        DataTable dt = DBHelper.ExecuteDataTable(sql);

        string fsmessage = "";
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"typename\":\"" + dt.Rows[i]["typename"] + "\"");
            sb.Append(",\"remark\":\"" + dt.Rows[i]["remark"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //修改
    public void edit(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string id = Utility.Trim(list.LoadData_type[0].id);
        string rolesname = Utility.Trim(list.LoadData_type[0].ROLESNAME);
        string rolesremark = Utility.Trim(list.LoadData_type[0].ROLESREMARK);
        string functionids = Utility.Trim(list.LoadData_type[0].FUNCTIONIDS);
        string functionnames = Utility.Trim(list.LoadData_type[0].FUNCTIONNAMES);
        string isactive = Utility.Trim(list.LoadData_type[0].ISACTIVE);
        string isactive_= isactive == "True" ? "Y" : "N";
        string ishave = "select * from sys_roles where rolesname='{0}' and ltrim(rtrim(isactive))='Y'";
        ishave = string.Format(ishave, rolesname);
        DataTable dtcount= DBHelper.ExecuteDataTable(ishave);
        if (dtcount.Rows.Count > 0 && dtcount.Rows[0]["id"].ToString() != id)
        {
            flag = false;
            msg = "已存在此权限名称，请修改";

        }
        else
        {
            sqlsave = @"update [sys_roles] set 
                                   [rolesname]='{0}'
                                  ,[rolesremark]='{1}'
                                  ,[functionids]='{2}'
                                  ,[functionnames]='{3}'
                                  ,[isactive]='{4}'
                                  ,[updatetime]='{5}'
                                   where id={6};";
            sqlsave = string.Format(sqlsave,rolesname, rolesremark,functionids,functionnames,isactive_, DateTime.Now.ToString(), id);
            int zxcount = DBHelper.ExecuteNonQuery(sqlsave);
            if (zxcount > 0)
            {
                flag = true;
                msg = "修改成功";
            }
            else
            {
                flag = false;
                msg = "修改失败，请联系管理员";
            }
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //新增
    public void add(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string rolesname = Utility.Trim(list.LoadData_type[0].rolesname);
        string rolesremark = Utility.Trim(list.LoadData_type[0].rolesremark);
        string functionids = Utility.Trim(list.LoadData_type[0].functionids);
        string functionnames = Utility.Trim(list.LoadData_type[0].functionnames);
        string isactive = Utility.Trim(list.LoadData_type[0].isactive);
        string isactive_= isactive == "True" ? "Y" : "N";

        string ishave = "select * from sys_roles where rolesname='{0}' and ltrim(rtrim(isactive))='Y'";
        ishave = string.Format(ishave, rolesname);
        DataTable dtcount= DBHelper.ExecuteDataTable(ishave);
        if (dtcount.Rows.Count > 0)
        {
            flag = false;
            msg = "已存在此权限名称，请修改";
        }
        else
        {
            sqlsave = @"INSERT INTO [sys_roles]
                                   ([rolesname]
                                   ,[rolesremark]
                                   ,[functionids]
                                   ,[functionnames]
                                   ,[isactive]
                                   ,[createtime]
                                   ,[updatetime]
                                   )
                             VALUES
                                   ('{0}'
                                   ,'{1}'
                                   ,'{2}'
                                   ,'{3}'
                                   ,'{4}'
                                   ,'{5}'
                                   ,'{6}'
                                  );";
            sqlsave = string.Format(sqlsave, rolesname, rolesremark,functionids,functionnames,isactive_, DateTime.Now.ToString(), DateTime.Now.ToString());
            int zxcount = DBHelper.ExecuteNonQuery(sqlsave);
            if (zxcount > 0)
            {
                flag = true;
                msg = "新增成功";
            }
            else
            {
                flag = false;
                msg = "新增失败，请联系管理员";
            }
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    public class GetDate
    {
        public List<rolesmanager> LoadData_type { get; set; }
    }
    public class rolesmanager
    {
        //id
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        public string rolesname;
        public string ROLESNAME
        {
            get { return rolesname; }
            set { rolesname = value; }
        }
        public string rolesremark;
        public string ROLESREMARK
        {
            get { return rolesremark; }
            set { rolesremark = value; }
        }
        public string functionids;
        public string FUNCTIONIDS
        {
            get { return functionids; }
            set { functionids = value; }
        }
        public string functionnames;
        public string FUNCTIONNAMES
        {
            get { return functionnames; }
            set { functionnames = value; }
        }
        public string isactive;
        public string ISACTIVE
        {
            get { return isactive; }
            set { isactive = value; }
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}